package priv.lhy.ribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author: lihy
 * date: 2019/9/11 13:17
 * description:
 */
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient client;

    @HystrixCommand(fallbackMethod = "hellFallback")
    public String helloService() {
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
    }

    @HystrixCommand(fallbackMethod = "hellFallback")
    public String selfHelloService() {
        ServiceInstance instance = client.choose("HELLO-SERVICE");
        return instance.getHost() + ":" + instance.getPort();
    }


    public String hellFallback() {
        return "error";
    }
}
